<template>
  <div>
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="旧密码" prop="oldPwd"
        ><el-input
          v-model="ruleForm.oldPwd"
          type="password"
          autocomplete="off"
          style="width: 300px"
        ></el-input
      ></el-form-item>
      <el-form-item label="新密码" prop="newPwd"
        ><el-input
          v-model="ruleForm.newPwd"
          type="password"
          autocomplete="off"
          style="width: 300px"
        ></el-input
      ></el-form-item>
      <el-form-item label="确认密码" prop="confirmPwd"
        ><el-input
          v-model="ruleForm.confirmPwd"
          type="password"
          autocomplete="off"
          style="width: 300px"
        ></el-input
      ></el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >提交</el-button
        >
        <el-button @click="resetForm(ruleFormRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { resetPwd } from "../../api/userCenter/index";
import { ElMessage, ElMessageBox } from "element-plus";
const ruleFormRef = ref<FormInstance>();
let ruleForm = reactive({
  oldPwd: "",
  newPwd: "",
  confirmPwd: "",
});
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入新密码"));
  } else {
    if (ruleForm.confirmPwd !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("confirmPwd", () => null);
    }
    callback();
  }
};
const validateConfirmPass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("请输入确认密码"));
  } else if (value !== ruleForm.newPwd) {
    callback(new Error("两次密码输入不一致"));
  } else {
    callback();
  }
};
const rules = reactive<FormRules>({
  oldPwd: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
  newPwd: [
    { validator: validatePass, trigger: "blur" },
    { min: 6, max: 20, message: "密码在6-20位之间", trigger: "blur" },
  ],
  confirmPwd: [{ validator: validateConfirmPass, trigger: "blur" }],
});

//提交
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      const res = await resetPwd({
        oldPwd: ruleForm.oldPwd,
        newPwd: ruleForm.newPwd,
        id: 1,
      });
      if (res.data.success) {
        ElMessage({
          message: res.data.msg,
          type: "success",
        });
      } else {
        ElMessage.error(res.data.msg);
      }
    } else {
      console.log("error submit!");
      return false;
    }
  });
};
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style></style>
